package com.hsav.kcxb;

import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.util.Log;

import com.hsav.kcxb.main.MSYS;
import com.kcbsdk.cjmkcs;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Timer;
import java.util.TimerTask;

import main.MAPI;

import static com.hsav.kcxb.main.MSYS.mMyTools;

public class BootActivity extends Activity
{
    private static final String TAG = BootActivity.class.getName();
    private int mTimer = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_power_on);

//        Log.e(TAG, "Callc: DDNSService.startService");
//        MainService.startService(this);

        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                //Log.e("tag","finish"+mTimer);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (++mTimer == 2) {
//                            MSYS.CheckLogin();
                            String downloadPath = MSYS.getDownloadPath();
                            CopyAssetsFile("AreaList.txt", downloadPath);
                            //mMyTools.Log(String.format("CopyAssetsFile %s %d ", downloadPath, MAPI.GET_FILE_LENGTH(downloadPath+"AreaList.txt")));
                        }
                        else if (mTimer > 19) {
                            MSYS.MSTRING("PowerOnActivity finish");
                            Log.e("tag","finish");
                            startActivity(new Intent(BootActivity.this, MainActivity.class));
//                            startActivity(new Intent(PowerOnActivity.this, MainInitActivity.class));
                            timer.cancel();
                            finish();
                        }
                    }
                });
            }
        }, 1, 100);
        initKcbSdk();
    }
    private void initKcbSdk()	{
        int g2FixServerIP = 0x8102; // 168 239
        int startPort = 1621;
        int fixMode = g2FixServerIP;
        String fixIPAddr = "";
        ;
        String gsLocal_1 = "";
        String gsLocal_2 = "";

        boolean pathExist = false;
        boolean pathCreate = false;
        File file;

        String appName = getString(R.string.appEnName);
        String gsFilePath = "";
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) == true)	{
            // 判断SDCard是否存在 [当没有外挂SD卡时，内置ROM也被识别为存在sd卡]
            gsFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + appName + "/";
            file = new File(gsFilePath);
            pathExist = file.exists();
            if (!pathExist)
            {
                pathCreate = file.mkdirs();
            }
            pathExist = file.exists();
        }
        if (!pathExist && !pathCreate)	{
            gsFilePath = getApplicationContext().getFilesDir() + "/" + appName + "/";
        }
        int version = 22;//VersionToDec32(MSYS.gsVersionName);
        gsLocal_2 = android.os.Build.MODEL + "&" + Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
        if (!cjmkcs.SYSTEM_STARTUP(version, appName, gsFilePath, gsLocal_2, (fixMode << 16) | startPort, fixIPAddr)){
            //	return;
        }
        int debugFlag = 0x11;
        cjmkcs.systemSetDebugFlag(debugFlag, 0);
        MSYS.SetStartUp();
    }
    private Boolean CopyAssetsFile(String filename, String des) {
        Boolean isSuccess = true;
        //复制安卓apk的assets目录下任意路径的单个文件到des文件夹，注意是否对des有写权限
        AssetManager assetManager = this.getAssets();

        InputStream in = null;
        OutputStream out = null;
        try {
            in = assetManager.open(filename);
            String newFileName = des + "/" + filename;
            out = new FileOutputStream(newFileName);
            byte[] buffer = new byte[1024];
            int read;
            while ((read = in.read(buffer)) != -1) {
                out.write(buffer, 0, read);
            }
            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;
        } catch (Exception e) {
            e.printStackTrace();
            isSuccess = false;
        }

        return isSuccess;

    }

}
